/**
 * Copyright (C) 2011 Andrew C. Love (DNC) <dnc.app.sup@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.dnc.cloak.framework.security;

import java.util.UUID;

import javax.security.auth.Subject;
import javax.security.auth.login.LoginContext;

import org.apache.log4j.Logger;


public class SecurityFactory {
	
	private static final Logger logger = Logger.getLogger(SecurityFactory.class.getName());

	private SecurityFactory() {
		super();	
		
		if(logger.isDebugEnabled()){
			logger.debug("SecurityFactory constructor");
		}
	}

    public static SecurityContext createSecurityContext(LoginContext lc ) throws SecurityException {
        logger.debug("Creating security context for:" + lc);
        return createSecurityContext(lc.getSubject());
    }

    public static SecurityContext createSecurityContext(Subject subject ) throws SecurityException {
        SecurityContext securityContext = new DefaultSecurityContext(subject);
        logger.debug("Created security context for:" + subject);
        return securityContext;
    }

    public static SessionContext createSession(LoginContext lc) throws SecurityException {
        return createSession(lc.getSubject());
    }

    public static SessionContext createSession(Subject subject) throws SecurityException {
        String sessionId = UUID.randomUUID().toString();
        DefaultSessionContext session = new DefaultSessionContext(sessionId);       
        logger.debug("Created sessionId:" + sessionId );
        logger.debug("Created session:" + subject );
        
        return session;
    }

    public static SessionContext createStatelessSession(LoginContext lc) throws SecurityException {
        return createStatelessSession(lc.getSubject());
    }   

    public static SessionContext createStatelessSession(Subject subject) throws SecurityException {
        String sessionId = UUID.randomUUID().toString();
        DefaultSessionContext session = new DefaultSessionContext(sessionId);       
        logger.debug("Created sessionId:" + sessionId );
        logger.debug("Created session:" + subject );
        
        return session;
    }   

    public static CloakPrincipal createPrincipal(String userName,  String password) throws SecurityException {
        CloakPrincipal cloakPrincipal = new CloakPrincipal();
        cloakPrincipal.setName(userName);
        cloakPrincipal.setPassword(password);
        logger.debug("Created principal:" + userName );
                
        return cloakPrincipal;
    }
 
}
